package com.shushan.permission;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.android.permission.rom.RomType;
import com.android.permission.rom.RomUtils;
import com.shushan.base.NavigationLoseActivity;
import com.shushan.shushanbao.R;
import com.shushan.ui.CustomDialog;
import com.shushan.util.OpenSettingUtils;

/**
 * @ClassName: CloseSysLockActivity
 * @Desciption: 关闭系统锁屏引导界面
 * @author: yichaohua
 * @date: 2018-05-08
 */
public class CloseSysLockActivity extends NavigationLoseActivity implements View.OnClickListener {

    private static final String TAG = "CloseSysLockActivity";

    private LinearLayout huaweiLayout;
    private LinearLayout oppoLayout;
    private LinearLayout vivoLayout;
    private LinearLayout xiaomiLayout;

    private Button nextButton;
    private Button okButton;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTitle("关闭系统锁屏");

        setContentView(R.layout.close_system_lock_activity);

        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();

        initData();
    }

    private void initView() {
        huaweiLayout = findViewById(R.id.close_system_lock_huawei_layout);
        oppoLayout = findViewById(R.id.close_system_lock_oppo_layout);
        vivoLayout = findViewById(R.id.close_system_lock_vivo_layout);
        xiaomiLayout = findViewById(R.id.close_system_lock_xiaomi_layout);
        nextButton = findViewById(R.id.close_system_lock_next_btn);
        nextButton.setOnClickListener(this);
        okButton = findViewById(R.id.close_system_lock_ok_btn);
        okButton.setOnClickListener(this);
    }

    private void initData() {
        RomType romType = RomUtils.getRomType(this);
        Log.i(TAG,"romType=" + romType.toString());

        switch (romType) {
            case MIUI_ROM:
                huaweiLayout.setVisibility(View.GONE);
                oppoLayout.setVisibility(View.GONE);
                vivoLayout.setVisibility(View.GONE);
                xiaomiLayout.setVisibility(View.VISIBLE);
                break;
            case EMUI_ROM:
                huaweiLayout.setVisibility(View.VISIBLE);
                oppoLayout.setVisibility(View.GONE);
                vivoLayout.setVisibility(View.GONE);
                xiaomiLayout.setVisibility(View.GONE);
                break;
            case COLOROS_ROM:
                huaweiLayout.setVisibility(View.GONE);
                oppoLayout.setVisibility(View.VISIBLE);
                vivoLayout.setVisibility(View.GONE);
                xiaomiLayout.setVisibility(View.GONE);
                break;
            case FUNTOUCHOS_ROM:
                huaweiLayout.setVisibility(View.GONE);
                oppoLayout.setVisibility(View.GONE);
                vivoLayout.setVisibility(View.VISIBLE);
                xiaomiLayout.setVisibility(View.GONE);
                break;
            default:
                break;
        }

    }

    @Override
    protected void onLeftBackward() {
        showBackDialog();
    }

    private void showBackDialog() {
        new CustomDialog.Builder(this)
                .setTitle("关闭系统锁屏")
                .setMessage("是否已关闭系统锁屏？")
                .setNegativeButton("已关闭", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                        onBackAction(400);
                    }
                })
                .setPositiveButton("未关闭", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                    }
                })
                .create().show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.close_system_lock_next_btn:
                OpenSettingUtils.gotoSystemLockSetting(this);
                break;
            case R.id.close_system_lock_ok_btn:
                onBackAction(400);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                showBackDialog();
                return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
